package com.ccepc.algmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ccepc.algmanage.service.PlcConfigService;
import com.ccepc.algmanage.utils.CommonUtils;
import com.ccepc.common.core.util.R;
import com.ccepc.common.log.annotation.SysLog;
import com.ccepc.common.security.annotation.Inner;
import com.ccepc.demo.api.dto.AlgorithmInfoDto;
import com.ccepc.demo.api.dto.PlcConfigDto;
import com.ccepc.demo.api.entity.PlcConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/plc" )
@Api(value = "plcConfig", tags = "plc配置")
public class PlcConfigController {
    @Resource
    private PlcConfigService plcConfigService;

    @ApiOperation(value = "PLC连接读取")
    @GetMapping("/read/{linkedId}" )
    @PreAuthorize("@pms.hasPermission('algmanage_plc_query')" )
    public R readPoint(@PathVariable("linkedId" ) String linkedId) {
        return plcConfigService.readPoint(linkedId);
    }

    @ApiOperation(value = "获取PLC配置信息")
    @GetMapping("/{linkedId}" )
    @PreAuthorize("@pms.hasPermission('algmanage_plc_query')" )
    public R getInfo(@PathVariable("linkedId" ) String linkedId) {
        return R.ok(plcConfigService.getOne(new LambdaQueryWrapper<PlcConfig>()
                .eq(PlcConfig::getLinkedId, linkedId)));
    }

    @SysLog("新增或修改PLC配置" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('algmanage_plc_update')" )
    public R save(@RequestBody PlcConfig plcConfig) {
        return R.ok(plcConfigService.saveOrUpdate(plcConfig));
    }

    @SysLog("修改PLC配置类型（0：自动控制，1：手动控制）" )
    @PutMapping("/status")
    @PreAuthorize("@pms.hasPermission('algmanage_plc_update')" )
    public R status(@RequestBody PlcConfig plcConfig) {
        return R.ok(plcConfigService.update(
                new LambdaUpdateWrapper<PlcConfig>()
                        .eq(PlcConfig::getLinkedId, plcConfig.getLinkedId())
                        .set(PlcConfig::getLdType, plcConfig.getLdType())
        ));
    }

    @SysLog("开启或关闭PLC" )
    @PostMapping("/changeStatus")
    @PreAuthorize("@pms.hasPermission('algmanage_plc_update')" )
    public R changeStatus(@RequestBody PlcConfigDto plcConfigDto) {
        return plcConfigService.changeStatus(plcConfigDto.getPlcConfig().getLinkedId(), plcConfigDto.getStatus(), true);
    }

}
